﻿#include "stdafx.h"
#include "Dm.h"

bool(CDm::* funcOcr[])(OcrInfo) = { &CDm::CmpCol,&CDm::FindCol,&CDm::FindMulCol,&CDm::FindPic1,&CDm::FindStrFast1 };

bool(CDm::* funAction[])(OcrInfo) = { &CDm::dmClick,&CDm::dmDoubleClick };

CDm::CDm()
{

}

CDm::~CDm()
{
}

long CDm::getX() {
	return this->m_x;
}

long CDm::getY() {
	return this->m_y;
}
CString CDm::RunScriptEx(CString strScript) {
	OcrInfo ocrInfo = ocrMap[strScript];
	CString arr[4];
	Split(ocrInfo.strPoint, arr, L",");
	//放大范围
	CString str = L"";
	long nEnlarge = _wtol(ocrInfo.strEnlarge);
	for (size_t i = 0; i < ocrInfo.nCount; i++)
	{
		switch (_wtoi(ocrInfo.strType))
		{
		case 5://多图序号
			str = FindPicExS(_wtoi(arr[0]) - nEnlarge, _wtoi(arr[1]) - nEnlarge, _wtoi(arr[2]) + nEnlarge, _wtoi(arr[3]) + nEnlarge, ocrInfo.strOcrText, ocrInfo.strOffsetCol, ocrInfo.fSim, 0);
			if (str != L"") {
				return str;
			}
		default:
			break;
		}
	}
	return L"";
}

CString CDm::RunScriptEx(CString strPoint, CString strData, int nType) {
	CString arr[4];
	Split(strPoint, arr, L",");
	CString str;

	switch (nType)
	{
	case 1:
		str = FindPicExS(_wtoi(arr[0]) - 5, _wtoi(arr[1]) - 5, _wtoi(arr[2]) + 5, _wtoi(arr[3]) + 5, strData, L"050505", 1, 0);
		break;

	}
	return str;
}
CString CDm::RunScriptEx(CString strPoint, CString strData, int nType, float fSim, CString strOffsetCol) {
	CString arr[4];
	Split(strPoint, arr, L",");
	CString str;
	switch (nType)
	{
	case 1:
		str = FindPicExS(_wtoi(arr[0]) - 5, _wtoi(arr[1]) - 5, _wtoi(arr[2]) + 5, _wtoi(arr[3]) + 5, strData, strOffsetCol, fSim, 0);
		break;

	}
	return str;
}
//runScriptExs("坐标", "数据", "类型")

bool CDm::RunScript(CString strPoint, CString strData, CString strType, CString strAction, CString strRnd, float fsim, CString strOffsetCol)
{
	OcrInfo ocrInfo;
	ocrInfo.strPoint = strPoint;
	ocrInfo.nCount = 1;
	ocrInfo.nDelay = 300;
	ocrInfo.strEnlarge = L"5";
	ocrInfo.strOcrText = strData;
	ocrInfo.fSim = fsim;
	ocrInfo.strOffsetCol = strOffsetCol;
	ocrInfo.strAction = strAction;
	ocrInfo.strType = strType;
	ocrInfo.strRndPoint = strRnd == L"" ? L"0,0" : strRnd;
	ocrInfo.strOffsetPoint = L"0,0";
	for (size_t i = 0; i < ocrInfo.nCount; i++)
	{
		if ((this->*funcOcr[_wtoi(ocrInfo.strType)])(ocrInfo)) {

			return true;
		}
	}
	m_x = -1;
	m_y = -1;
	return false;
}

bool CDm::RunScript(CString strPoint, CString strData, CString strType, CString strAction, CString strRnd) {
	OcrInfo ocrInfo;
	ocrInfo.strPoint = strPoint;
	ocrInfo.nCount = 1;
	ocrInfo.nDelay = 300;
	ocrInfo.strEnlarge = L"5";
	ocrInfo.strOcrText = strData;
	ocrInfo.fSim = 1.0;
	ocrInfo.strOffsetCol = L"050505";
	ocrInfo.strAction = strAction;
	ocrInfo.strType = strType;
	ocrInfo.strRndPoint = strRnd == L"" ? L"0,0" : strRnd;
	ocrInfo.strOffsetPoint = L"0,0";
	for (size_t i = 0; i < ocrInfo.nCount; i++)
	{
		if ((this->*funcOcr[_wtoi(ocrInfo.strType)])(ocrInfo)) {

			return true;
		}
	}
	m_x = -1;
	m_y = -1;
	return false;
}
bool CDm::RunScript(CString strScript, long* x, long* y)
{
	OcrInfo ocrInfo = ocrMap[strScript];
	for (size_t i = 0; i < ocrInfo.nCount; i++)
	{
		if ((this->*funcOcr[_wtoi(ocrInfo.strType)])(ocrInfo)) {
			*x = m_x;
			*y = m_y;
			return true;
		}
	}
	m_x = -1;
	m_y = -1;
	return false;
}
bool CDm::RunScript(CString strScript)
{
	OcrInfo ocrInfo = ocrMap[strScript];
	for (size_t i = 0; i < ocrInfo.nCount; i++)
	{
		if ((this->*funcOcr[_wtoi(ocrInfo.strType)])(ocrInfo)) {

			return true;
		}
		g_dm->delay(ocrInfo.nDelay);
	}
	m_x = -1;
	m_y = -1;
	return false;
}


bool CDm::RunScript(CString strScript, CString strData) {//动态数据
	OcrInfo ocrInfo = ocrMap[strScript];
	ocrInfo.strOcrText = strData;
	for (size_t i = 0; i < ocrInfo.nCount; i++)
	{
		if ((this->*funcOcr[_wtoi(ocrInfo.strType)])(ocrInfo)) {

			return true;
		}
	}
	m_x = -1;
	m_y = -1;
	return false;
}

CString CDm::RunScripts(CString strScript, CString strData) {//找多图，外部传入数据
	OcrInfo ocrInfo = ocrMap[strScript];
	ocrInfo.strOcrText = strData;
	CString arr[4];
	Split(ocrInfo.strPoint, arr, L",");
	CString str;
	long nEnlarge = _wtol(ocrInfo.strEnlarge);
	for (size_t i = 0; i < ocrInfo.nCount; i++)
	{
		switch (_wtoi(ocrInfo.strType))
		{
		case 5:
			str = FindPicEx(_wtoi(arr[0]) - nEnlarge, _wtoi(arr[1]) - nEnlarge, _wtoi(arr[2]) + nEnlarge, _wtoi(arr[3]) + nEnlarge, ocrInfo.strOcrText, ocrInfo.strOffsetCol, ocrInfo.fSim, 0);
			break;
		case 6:
			str = FindPicExS(_wtoi(arr[0]) - nEnlarge, _wtoi(arr[1]) - nEnlarge, _wtoi(arr[2]) + nEnlarge, _wtoi(arr[3]) + nEnlarge, ocrInfo.strOcrText, ocrInfo.strOffsetCol, ocrInfo.fSim, 0);
			break;
		default:
			break;
		}
	}
	return str;
}


bool CDm::RunScript(CString strScript, int nType, CString strData) {
	switch (nType)
	{
	case 1:
		//找到后输入字符串
		if (RunScript(strScript)) {
			this->Delays(1800, 2000);
			SendString2(GetBindWindow(), strData);
			return true;
		}
		break;
	case 2:
		this->Delays(500, 1000);
		if (RunScript(strScript, strData)) {//识别相同范围不同数据
			return true;
		}
	default:
		break;
	}
	return false;
}

bool CDm::dmClick(OcrInfo ocrInfo) {
	POINT pointRnd;
	pointRnd.x = _wtol(ocrInfo.strRndPoint.Mid(0, ocrInfo.strRndPoint.Find(L",")));
	pointRnd.y = _wtol(ocrInfo.strRndPoint.Mid(ocrInfo.strRndPoint.Find(L",") + 1));
	POINT point;
	point.x = _wtol(ocrInfo.strOffsetPoint.Mid(0, ocrInfo.strOffsetPoint.Find(L",")));
	point.y = _wtol(ocrInfo.strOffsetPoint.Mid(ocrInfo.strOffsetPoint.Find(L",") + 1));
	MoveToEx(m_x + point.x, m_y + point.y, pointRnd.x, pointRnd.y);
	LeftClick();
	return true;
}
bool CDm::dmDoubleClick(OcrInfo ocrInfo) {
	dmClick(ocrInfo);
	LeftClick();
	return true;
}
bool CDm::FindMulCol(OcrInfo ocrInfo) { return true; }
bool CDm::CmpCol(OcrInfo ocrInfo) { return true; }
bool CDm::FindCol(OcrInfo ocrInfo) {
	return true;
}

bool CDm::FindPic1(OcrInfo ocrInfo) {
	CString arr[4];
	Split(ocrInfo.strPoint, arr, L",");
	//放大范围
	long nEnlarge = _wtol(ocrInfo.strEnlarge);

	dmsoft::FindPic(_wtoi(arr[0]) - nEnlarge, _wtoi(arr[1]) - nEnlarge, _wtoi(arr[2]) + nEnlarge, _wtoi(arr[3]) + nEnlarge, ocrInfo.strOcrText, ocrInfo.strOffsetCol, ocrInfo.fSim, 0, &this->m_x, &this->m_y);
	if (this->m_x > 0) {//找到数据
		if (_wtoi(ocrInfo.strAction) > 0) {
			if ((this->*funAction[_wtoi(ocrInfo.strAction) - 1])(ocrInfo)) {
				return true;
			}
		}
		return true;
	}
	return false;
}

bool CDm::FindStrFast1(OcrInfo ocrInfo) {
	CString arr[4];
	Split(ocrInfo.strPoint, arr, L",");
	//放大范围
	long nEnlarge = _wtol(ocrInfo.strEnlarge);
	FindStrFast(_wtoi(arr[0]) - nEnlarge, _wtoi(arr[1]) - nEnlarge, _wtoi(arr[2]) + nEnlarge, _wtoi(arr[3]) + nEnlarge, ocrInfo.strOcrText, ocrInfo.strOffsetCol, ocrInfo.fSim, &this->m_x, &this->m_y);
	//CString ocr = this->Ocr(_wtoi(arr[0]) - nEnlarge, _wtoi(arr[1]) - nEnlarge, _wtoi(arr[2]) + nEnlarge, _wtoi(arr[3]) + nEnlarge, L"000000",1);
	if (this->m_x > 0) {//找到数据
		if (_wtoi(ocrInfo.strAction) > 0) {
			if ((this->*funAction[_wtoi(ocrInfo.strAction) - 1])(ocrInfo)) {
				return true;
			}
		}
		return true;
	}
	return false;
}